package com.shj.em.biz.prod.controller;

import com.shj.em.biz.prod.entity.OrderCnc;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;
import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.shj.em.common.annotation.Log;
import com.shj.em.base.support.ResMsg;
import com.shj.em.base.vo.PageVO;

import com.shj.em.biz.prod.entity.Knife;
import com.shj.em.biz.prod.service.KnifeService;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.extern.slf4j.Slf4j;
import com.shj.em.base.controller.BaseController;


/**
 * <p>
 * 刀具表 前端控制器
 * </p>
 *
 * @author Shen Huang Jian
 * @since 2018-12-16
 */
@Controller
@Slf4j
@RequestMapping("/prod/knife")
public class KnifeController extends BaseController<Knife>{

    private String prefix = "prod/knife";

    @Autowired
    KnifeService knifeService;

    @GetMapping("/gotoSearch")
    public String gotoSearch(){
        return prefix + "/search";
    }

    @GetMapping("/list")
    @ResponseBody
    public PageVO<Knife> findKnife(@RequestParam("cncId") Long cncId){
        List<Knife> list = knifeService.findKnifeByCncId(cncId);
        PageVO<Knife> pageVO = new PageVO<>();
        pageVO.setTotal(list.size());
        pageVO.setRows(list);
        return pageVO;
    }

    @GetMapping("/gotoAdd/{cncId}")
	String gotoAdd(@PathVariable("cncId")Long cncId, Model model){
        Knife knife = new Knife();
        knife.setCncId(cncId);
        model.addAttribute("isEdit", false);
        model.addAttribute("knife", knife);
	    return prefix + "/add";
	}

	@GetMapping("/gotoEdit/{id}")
	String gotoEdit(@PathVariable("id") Long id, Model model){
        Knife knife = knifeService.selectById(id);
		model.addAttribute("knife", knife);
        model.addAttribute("isEdit", true);
	    return prefix + "/add";
	}

	@Log("创建刀具表")
	@ResponseBody
	@PostMapping("/add")
	public ResMsg add(Knife knife){
        try{
            knifeService.insert(knife);
            return ResMsg.ok(new Knife());
        }catch(Exception e){
            log.error("创建刀具表失败", e);
            return ResMsg.error("创建刀具表失败");
        }

	}

    @Log("更新刀具表")
	@ResponseBody
	@PostMapping("/edit")
	public ResMsg edit(Knife knife){
        try{
            knifeService.updateById(knife);
            return ResMsg.ok();
        }catch(Exception e){
            log.error("更新刀具表失败", e);
            return ResMsg.error("更新刀具表失败");
        }

	}

	@Log("批量删除刀具表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	public ResMsg batchRemove(@RequestParam("ids[]") Long[] ids){
        knifeService.deleteBatchIds(Arrays.asList(ids));
		return ResMsg.ok();
	}

}

